package sixth.group.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sixth.group.dao.*;
import sixth.group.domain.*;
import sixth.group.service.WyxAdministratorService;

import java.util.List;
import java.util.stream.Collectors;


//此处需要添加注解使IOC容器能够将该类的实例对象作为组件扫描到容器中
@Service
public class WyxAdministratorServiceImpl implements WyxAdministratorService{

    @Autowired
    private RestaurantMapper restaurantMapper;//自动注入，  看到存在成员时，如果有Autowired注解修饰，则从IOC容器取出对象使用
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private DishesMapper dishesMapper;
    @Autowired
    private OrderinfMapper orderinfMapper;
    @Autowired
    private AdministratorMapper administratorMapper;

    @Override
    public boolean WyxAddRes(Restaurant restaurant){
        int i=restaurantMapper.insertSelective(restaurant);
        System.out.println("添加了"+i+"个res");
        return true;

    }

    @Override
    public List<Restaurant> resLook() {
        //WYX
        List<Restaurant> restaurants = restaurantMapper.selectByExample(null);
        return restaurants;
    }

    @Override
    public Integer resDelete(int rid) {
            //WYX
            //先删除副表对应数据
            DishesExample dishesExample = new DishesExample();
            DishesExample.Criteria criteria = dishesExample.createCriteria();
            criteria.andDridEqualTo(rid);

            List<Dishes> dishes =dishesMapper.selectByExample(dishesExample);

            //删除dishes中的属性需要先删除dishes的副表ord_dis_match
            //获取order LIST中的did属性
            List dishesDids =dishes.stream().map(Dishes::getDid).collect(Collectors.toList());
            int orderinfsSize=dishes.size(); //取到了几个rid匹配的dishes
             //循环根据不同oid删除ord_dis_match
             for(int deleteflag=0; deleteflag<orderinfsSize;deleteflag++) {
            int did= (int) dishesDids.get(deleteflag);
            administratorMapper.deleteBYWyxdorderid(did);
             }

            dishesMapper.deleteByExample(dishesExample);

            OrderinfExample orderinfExample = new OrderinfExample();
            OrderinfExample.Criteria  Ocriteria= orderinfExample.createCriteria();
            Ocriteria.andOresaurantidEqualTo(rid);
            orderinfMapper.deleteByExample(orderinfExample);

            AdministratorExample administratorExample =new AdministratorExample();
            OrderinfExample.Criteria  Acriteria= orderinfExample.createCriteria();
            Acriteria.andOresaurantidEqualTo(rid);
            administratorMapper.deleteByWyxrrestaurantid(rid);

            //再删主表数据
        int i = restaurantMapper.deleteByPrimaryKey(rid);
        return i;
    }


    @Override
    public List<Customer> cusLook() {
        //WYX
        List<Customer> customers =customerMapper.selectByExample(null);
        return customers;
    }

    @Override
    public Integer cusDelete(Integer cid) {
        //WYX
        //先删除副表对应数据;
        //删除custormer表的副表oderinf
        OrderinfExample orderinfExample = new OrderinfExample();
        OrderinfExample.Criteria  Ocriteria= orderinfExample.createCriteria();
        Ocriteria.andOcustomeridEqualTo(cid);
        List<Orderinf> orderinfs =orderinfMapper.selectByExample(orderinfExample);

       //删除orderinf中的属性需要先删除oderinf的副表ord_dis_match
        //获取order LIST中的oid属性
        List orderOids =orderinfs.stream().map(Orderinf::getOid).collect(Collectors.toList());
        int orderinfsSize=orderinfs.size(); //取到了几个cid匹配的Orderinf
        //循环根据不同oid删除ord_dis_match
        for(int deleteflag=0; deleteflag<orderinfsSize;deleteflag++) {
            int oid= (int) orderOids.get(deleteflag);
            administratorMapper.deleteBYWyxdorderid(oid);
        }
        //现在才删除orderinf
        orderinfMapper.deleteByExample(orderinfExample);

        administratorMapper.deleteBYWyxrcustomerid(cid);
        //再删主表数据
        int i = customerMapper.deleteByPrimaryKey(cid);
        return i;
    }



}